package net.xdclass.online_xdclass.service.impl;

import net.xdclass.online_xdclass.model.entity.User;
import net.xdclass.online_xdclass.mapper.UserMapper;
import net.xdclass.online_xdclass.service.UserService;
import net.xdclass.online_xdclass.utils.CommonUtils;
import net.xdclass.online_xdclass.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {

    @Autowired(required=false)
    private UserMapper userMapper;

    @Override
    public List<User> listUser() {
        return userMapper.listUser();
    }


    @Override
    public int save(Map<String, String> userInfo) {

        User user = parseToUser(userInfo);
        if( user != null){
           return userMapper.save(user);
        }else {
            return -1;
        }

    }


    @Override
    public String findByPhoneAndPwd(String phone, String pwd) {

        User user = userMapper.findByPhoneAndPwd(phone, CommonUtils.MD5(pwd));

        if(user == null){
            return null;

        }else {
            String token = JWTUtils.geneJsonWebToken(user);
            return token;
        }

    }
    @Override
    public User findByNewTest(String newTest) {

        User user = userMapper.findByNewTest(newTest);
        return user;
    }

    @Override
    public User findByNewArea(String newArea) {

        User user = userMapper.findByNewArea(newArea);
        return user;
    }


    @Override
    public User findByUserId(Integer userId) {

        User user = userMapper.findByUserId(userId);
        return user;
    }

    /**
     * 解析 user 对象
     * @param userInfo
     * @return
     */
    private User parseToUser(Map<String,String> userInfo) {

        if(userInfo.containsKey("phone") && userInfo.containsKey("pwd") && userInfo.containsKey("name")){
            User user = new User();
            user.setName(userInfo.get("name"));
            user.setHeadImg(getRandomImg());
            user.setCreateTime(new Date());
            user.setPhone(userInfo.get("phone"));
            String pwd = userInfo.get("pwd");
            //MD5加密
            user.setPwd(CommonUtils.MD5(pwd));
            user.setNewTest("暂无");
            System.out.println(user);
            return user;
        }else {
            return null;
        }

    }

    /**
     * 放在CDN上的随机头像
     */
    private static final String [] headImg = {
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=326427685,3832691076&fm=26&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=326427685,3832691076&fm=26&gp=0.jpg",
            "https://avatar.csdnimg.cn/8/3/8/1_icebns.jpg",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=326427685,3832691076&fm=26&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=326427685,3832691076&fm=26&gp=0.jpg"
    };

    private String getRandomImg(){
        int size =  headImg.length;
        Random random = new Random();
        int index = random.nextInt(size);
        return headImg[index];
    }

    public int deleteUserById(Integer id) {
        System.out.println("id = [" + id + "]");
        int user = userMapper.deleteUserById(id);

        System.out.println("ArticleServiceImpl:"+user);
        return user;
    }

    @Override
    public int updateUser(Map<String, String> userInfo){
        User user = parseToUpdateUser(userInfo);
        if( user != null){
            return userMapper.updateUser(user);
        }else {
            return -1;
        }
    }

    private User parseToUpdateUser(Map<String, String> articleInfo) {
        System.out.println("articleInfo = [" + articleInfo + "]");
        if (articleInfo.containsKey("id") && articleInfo.containsKey("user_level")){
            User user = new User();
            user.setId(Integer.valueOf(articleInfo.get("id")));
            user.setUserLevel(Integer.valueOf(articleInfo.get("user_level")));
            System.out.println(user);
            return user;
        }else {
            return null;
        }
    }

}
